<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区域 -->
      <SubFilter @filter-change="filterChange" />
      <!-- 商品排序 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sort-change="sortChange" />
        <!-- 列表 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id">
            <GoodsItem :goods="goods" />
          </li>
        </ul>
        <XtxInfiniteLoading
          :loading="loading"
          :findshed="findshed"
          @infinite="getData"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from "vue-demi";
import { useRoute } from "vue-router";

import SubBread from "./components/sub-bread.vue";
import SubFilter from "./components/sub-filter.vue";
import SubSort from "./components/sub-sort.vue";
import GoodsItem from "./components/goods-item.vue";
import { findSubCategoryGoods } from "@/api/category";

export default {
  name: "categorySub",
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup(props) {
    const route = useRoute();
    const goodsList = ref([]);
    const loading = ref(false);
    const findshed = ref(false);
    // 请求参数
    let reqParams = {
      page: 1,
      pageSize: 20,
    };
    const getData = async () => {
      loading.value = true;
      reqParams.categoryId = route.params.id;
      const { result } = await findSubCategoryGoods(reqParams);
      if (result.items.length) {
        // 有数据  追加到数据中
        goodsList.value = [...goodsList.value, ...result.items];
        reqParams.page++;
      } else {
        // 没有数据  (加载到最后一页)
        findshed.value = true;
      }
      loading.value = false;
    };

    watch(
      () => route.params.id,
      (newValue) => {
        if (newValue && `/category/sub/${newValue}` === route.path) {
          // 重置请求数据
          reqParams.value = {
            page: 1,
            pageSize: 20,
          };
          loading.value = false;
          findshed.value = false;
          goodsList.value = []; // 列表为空 触发XtxInfiniteLoading组件见底 重新加载数据
        }
      }
    );

    // 更改排序组件的筛选数据  重新发送请求
    const sortChange = (sortParams) => {
      findshed.value = false;
      // 更改请求参数
      reqParams.value = { ...reqParams.value, ...sortParams };
      reqParams.page = 1;
      goodsList.value = []; // 列表为空 触发XtxInfiniteLoading组件见底 重新加载数据
    };

    // 更改筛选组件的筛选数据  重置新发送请求
    const filterChange = (filterParams) => {
       findshed.value = false;
      // 更改请求参数
      reqParams.value = { ...reqParams.value, ...filterParams };
      reqParams.page = 1;
      goodsList.value = []; 
    };

    return { getData, loading, findshed, goodsList, sortChange, filterChange };
  },
};
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>